package com.base.scanner;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import com.base.R;

public class AudioPlayerActivity extends AppCompatActivity implements OnVoicePlayerCallBack {

    private TextView tvTotal;
    private TextView tvProgress;
    private SeekBar seekBar;
    private String filePath;
    private Button btnStart;
    private AudioPlayer voicePlayer;
    boolean isUserTouch = false;

    public static void start(Context context, String filePath) {
        Intent intent = new Intent(context, AudioPlayerActivity.class);
        intent.putExtra(FileAdapter.FILE_KEY, filePath);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio_detail);


        filePath = getIntent().getStringExtra(FileAdapter.FILE_KEY);
        tvProgress = findViewById(R.id.tv_current_position);
        seekBar = findViewById(R.id.seekbar);
        tvTotal = findViewById(R.id.tv_total);
        btnStart = findViewById(R.id.btn_start);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GlobalAudioPlayerManager.playOrPause();
            }
        });

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvProgress.setText(FileUtils.stringForTime(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isUserTouch = true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                isUserTouch = false;
                if (voicePlayer == null) {
                    return;
                }
                if (!voicePlayer.isPrepared()) {
                    voicePlayer.setSeekOnStart(seekBar.getProgress());
                } else {
                    voicePlayer.seekTo(seekBar.getProgress());
                }
                voicePlayer.play();

            }
        });

        initAudioPlayer();
    }


    private void initAudioPlayer() {
        voicePlayer = new AudioPlayer();
        voicePlayer.setDataSourceAndPrepareAsync(filePath);
        GlobalAudioPlayerManager.replaceGlobalAudioPlayer(voicePlayer);

        GlobalAudioPlayerManager.addPlayCallBack(this);
    }


    @Override
    public void onAudioProgress(int progress, int max) {
        if (isUserTouch) {
            return;
        }
        updateMaxDuration(max);
        if (btnStart != null) {
            btnStart.setText("暂停");
        }

        seekBar.setProgress(progress);
        tvProgress.setText(FileUtils.stringForTime(progress));
    }

    @Override
    public void onAudioDuration(int duration) {
        updateMaxDuration(duration);
        if (btnStart != null) {
            btnStart.setText("暂停");
        }
    }

    @Override
    public void onAudioPause() {
        if (btnStart != null) {
            btnStart.setText("播放");
        }

    }

    @Override
    public void onAudioError(int what, int extra) {
        onAudioReset();
    }

    @Override
    public void onAudioCompletion() {
        onAudioReset();
        if (btnStart != null) {
            btnStart.setText("重播");
        }
    }

    @Override
    public void onAudioPlaying() {
        if (btnStart != null) {
            btnStart.setText("暂停");
        }

    }

    @Override
    public void onAudioReset() {
        seekBar.setProgress(0);
        tvProgress.setText(FileUtils.stringForTime(0));
        onAudioPause();
    }

    private void updateMaxDuration(int duration) {
        if (seekBar.getMax() == 0 || (duration > 0 && duration != seekBar.getMax())) {
            seekBar.setMax(duration);
            tvTotal.setText(FileUtils.stringForTime(duration));
        }
    }

    @Override
    public void finish() {
        super.finish();
        GlobalAudioPlayerManager.removePlayCallBack(this);
        GlobalAudioPlayerManager.clearGlobalAudioPlayer();
    }

}